package com.androidcare;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class Actualiza extends Service {
	@Override
	public void onCreate(){
	}
	@Override
	public void onStart(Intent intent, int startId){
		
		int numero_pendings = 0;
		PendingIntent pendingIntent;
		Intent myIntent =  new Intent(this,MyAlarmServiceRepeating.class);
		try {
			FileInputStream fIn = getBaseContext().openFileInput("PendingFile.txt");
			DataInputStream dis = new DataInputStream(fIn);
			BufferedReader br = new BufferedReader(new InputStreamReader(dis));
			numero_pendings=Integer.valueOf(br.readLine());
			Log.i("Actualiza","Numero Pendings: "+ String.valueOf(numero_pendings));
			fIn.close();
			dis.close();
			br.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for(int i=0;i<numero_pendings;i++){
	    	pendingIntent = PendingIntent.getService(this, i, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
	    	AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);				         		
	    	alarmManager.cancel(pendingIntent); 
			Log.i("Actualizacion","numero pending cancelado:"+String.valueOf(i));
		}
		Intent reinicio = new Intent(getApplicationContext(),Reinicio.class);
		startService(reinicio);
		Log.i("Actualiza","Llama a Reinicio");
		stopSelf();
	}
	@Override
	public IBinder onBind(Intent arg0) {
		// TODO Auto-generated method stub
		return null;
	}

}
